package com.apigateway.stateCount;

import apigateway.serviceRegister.ServiceNode;
import com.apigateway.mapper.ApiServiceCallMapper;
import com.apigateway.mapper.ApiServiceMapper;
import com.apigateway.model.ApiService;
import com.apigateway.model.ApiServiceCall;
import io.netty.buffer.UnpooledUnsafeDirectByteBuf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by 小泽玛丽罗 on 2017/3/12.
 */
@Component("stateCountUtil")
public class StateCountUtil {

    private static AtomicInteger id=new AtomicInteger(0);

    @Autowired
    private  ApiServiceCallMapper apiServiceCallMapper;
    @Autowired
    private   ApiServiceMapper apiServiceMapper;
    private static StateCountExceute stateCountExceute;

    public  void stateCount(long time, long proxyTime
            ,String uuid, ServiceNode serviceNode){
        if(serviceNode!=null){
            List<ApiService> apiServices=apiServiceMapper.findServices(serviceNode);
            ApiServiceCall apiServiceCall=new ApiServiceCall();
            apiServiceCall.setCalltime( new Date(time));
            if(null==uuid)
                uuid=generaterId();
            apiServiceCall.setUuid(uuid);
            apiServiceCall.setCosttime((int) (proxyTime));
            if(apiServices.size()!=0)
                apiServiceCall.setServiceid(apiServices.get(0).getId());
            StateCountRunnable stateCountRunnable=new StateCountRunnable(apiServiceCall,apiServiceCallMapper);
            stateCountExceute.stateCount(stateCountRunnable);
        }
    }

    public  String generaterId(){
        return System.currentTimeMillis()+"."+id.getAndIncrement();
    }


}
